
clear all
close all
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                  SAVE OUTPUTS TO CSV FILE                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

addpath('helpers')


basic_parameters;
version = {'','_sh'};
versionName = {'original','shelf'};

myList = struct;
myList.NumSims = numsims;
myList.NumMarkets = nmarkets;
save_scalars(myList, '../output/product/scalars/num_sims', '', false);

Z  = length(instruments);
K  = 2*length(strength_D)+length(strength_X)-1;
K1 = length(strength_D);
K2 = length(strength_X)-1;
V  = length(version);

results = struct;


varTypes_full = {'string','string','string','string','double','double','double','double','double','double','double','double','double','double'};
varNames_full = {'Model','Version','MisspecType','Instrument','Distance','Distance_group','Seed','own_elast_true','own_elast_hat','dccs_L','dccs_RCL','dcsx_Lnom','dcsx_L','dcsx_RCL'};
markets = [10000];
tolerance_list = ["default_tolerance"];


CSV_full      = table('Size',[0,length(varNames_full)],'VariableTypes',varTypes_full,'VariableNames',varNames_full);
models_name_main       = models;

for v=1:V
    vers = version{v};
    versName = versionName{v};
%%
    for d=1:K
        load(['../output/product/exports/sim_results_k',int2str(d),'_m10000',vers,'.mat']);
        results.(['r',int2str(d)]) = sim_results;

        own_elast             = permute(sim_results.mean_own_elasticity,[1,3,2]);
        dccs_L                = permute(sim_results.dccs_L,[1,3,2]);
        dccs_RCL              = permute(sim_results.dccs_RCL,[1,3,2]);
        dcsx_Lnom             = permute(sim_results.dcsx_Lnom,[1,3,2]);
        dcsx_L                = permute(sim_results.dcsx_L,[1,3,2]);
        dcsx_RCL              = permute(sim_results.dcsx_RCL,[1,3,2]);

        instruments_all       = instruments;
        legendlab_all         = instruments_labs(cell2mat(arrayfun( @(x) find(strcmp(instruments, instruments_all{x})),1:length(instruments_all),'un',0)));

        if (d >= 1 && d <= K1)
            iter = d;
            misspectype = ["X_and_D"];
            for zz=1:length(instruments_all)
                for kk=1:length(models_name_main) 
                    z = find(strcmp(sim_results.instruments,instruments_all{zz}));
                    k = kk +1;
                    for i = 1:numsims
                        CSV_full = [CSV_full;table(models_name_main(kk),string(versName), misspectype ,legendlab_all(zz), strength_D(iter), d, i, own_elast(1,z,i), own_elast(k,z,i), dccs_L(1,z,i), dccs_RCL(1,z,i), dcsx_Lnom(1,z,i), dcsx_L(1,z,i), dcsx_RCL(1,z,i),  'VariableNames', varNames_full)];
                    end
                end
            end
        end

        if (d >= K1+1 && d <= K1+K2)
            iter = K1+K2 - d + 1;
            misspectype = ["X_only"];
            for zz=1:length(instruments_all)
                for kk=1:length(models_name_main) 
                    z = find(strcmp(sim_results.instruments,instruments_all{zz}));
                    k = kk +1;
                    for i = 1:numsims
                        CSV_full = [CSV_full;table(models_name_main(kk),string(versName), misspectype, legendlab_all(zz), strength_X(iter), d, i, own_elast(1,z,i), own_elast(k,z,i), dccs_L(1,z,i), dccs_RCL(1,z,i), dcsx_Lnom(1,z,i), dcsx_L(1,z,i), dcsx_RCL(1,z,i),  'VariableNames', varNames_full)];
                    end
                end
            end
        end

        if (d >= K1+K2+1 && d <= K)
            iter = d - K1 - K2;
            misspectype = ["X_and_D_qrt"];
            for zz=1:length(instruments_all)
                for kk=1:length(models_name_main) 
                    z = find(strcmp(sim_results.instruments,instruments_all{zz}));
                    k = kk +1;
                    for i = 1:numsims
                        CSV_full = [CSV_full;table(models_name_main(kk),string(versName), misspectype, legendlab_all(zz), strength_D(iter), d, i, own_elast(1,z,i), own_elast(k,z,i), dccs_L(1,z,i), dccs_RCL(1,z,i), dcsx_Lnom(1,z,i), dcsx_L(1,z,i), dcsx_RCL(1,z,i),  'VariableNames', varNames_full)];
                    end
                end
            end
        end

        if (d == 1)
            iter = K2 + 1;
            misspectype = ["X_only"];
            for zz=1:length(instruments_all)
                for kk=1:length(models_name_main) 
                    z = find(strcmp(sim_results.instruments,instruments_all{zz}));
                    k = kk +1;
                    for i = 1:numsims
                        CSV_full = [CSV_full;table(models_name_main(kk),string(versName), misspectype, legendlab_all(zz), strength_X(iter), d, i, own_elast(1,z,i), own_elast(k,z,i), dccs_L(1,z,i), dccs_RCL(1,z,i), dcsx_Lnom(1,z,i), dcsx_L(1,z,i), dcsx_RCL(1,z,i),   'VariableNames', varNames_full)];
                    end
                end
            end
        end
    end
end

%%
writetable(CSV_full, ['../output/ExportCSVdraft.csv']);









